package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.DbContext;
import com.singfan.common.db.PrimaryKeyBuilder;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.IBarberDao;
import com.singfan.core.dao.database.model.Barber;
import com.singfan.core.dao.database.model.constant.BusinessStatusType;
import com.singfan.core.dao.database.model.constant.FilterType;
import com.singfan.core.dao.database.model.constant.HairServiceType;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class BarberDaoImpl extends DaoBaseGeneric<Barber> implements IBarberDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void create(Barber barber) {
        if (barber.getId() <= 0){
            barber.setId(PrimaryKeyBuilder.getInstance().build(DbContext.getDbConfigProvider().getTable(tableNameBase).getId(), getTableNewestVersion()));
        }

        insert(null, barber);
    }

    @Override
    public List<Barber> selectBarbersByShopId(long shopId, int serviceType) {
        String sql = "SELECT * FROM " + getTableName(shopId) + " WHERE shop_id=? AND status=? ";
        List<Object> paramList = new ArrayList<>();
        paramList.add(shopId);
        paramList.add(BusinessStatusType.VERIFY_PASSED);
        if (HairServiceType.isHairServiceType(serviceType)){
            if (serviceType == HairServiceType.HAIR_CUT){
                sql += "AND hair_cut=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_PERM){
                sql += "AND hair_perm=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_DYE){
                sql += "AND hair_dye=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_NURSE){
                sql += "AND hair_nurse=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_MODELING){
                sql += "AND hair_modeling=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_MAN_30){
                sql += "AND hair_cut_real_price>=3000 AND hair_cut_real_price<6000";
            }
            else if (serviceType == HairServiceType.HAIR_MAN_60){
                sql += "AND hair_cut_real_price>=6000 AND hair_cut_real_price<9000";
            }
            else if (serviceType == HairServiceType.HAIR_MAN_90){
                sql += "AND hair_cut_real_price>9000";
            }
        }

        return selectList(shopId, sql, paramList);
    }

    @Override
    public List<Barber> selectBarberList(List<Long> barberIdList) {
        int barberCount = barberIdList.size();
        List<Barber> barberList = new ArrayList<>(barberCount);
        List<Object> paramList = new ArrayList<>();
        paramList.add(BusinessStatusType.VERIFY_PASSED);
        if (barberCount > 0){
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=? AND id in (";
            for (int i=0; i<barberCount; i++){
                if (i == 0){
                    sql += "?";
                }
                else {
                    sql += ",?";
                }

                paramList.add(barberIdList.get(i));
            }

            sql += ")";

            barberList = selectList(null, sql, paramList);
        }

        return barberList;
    }

    @Override
    public List<Barber> selectBarberList(int serviceType, int listOrderType, int cityId, int businessCircleId, int pageIndex, int pageSize, int position) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE ";
        List<Object> paramList = new ArrayList<>();
        if (businessCircleId > 0){
            sql += "business_circle_id=? ";
            paramList.add(businessCircleId);
        }
        else {
            sql += "city_id=? ";
            paramList.add(cityId);
        }
        if (position > 0){
            sql += "AND position_id=? ";
            paramList.add(position);
        }

        if (HairServiceType.isHairServiceType(serviceType)){
            if (serviceType == HairServiceType.HAIR_CUT){
                sql += "AND hair_cut=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_PERM){
                sql += "AND hair_perm=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_DYE){
                sql += "AND hair_dye=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_NURSE){
                sql += "AND hair_nurse=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_MODELING){
                sql += "AND hair_modeling=?";
                paramList.add(true);
            }
            else if (serviceType == HairServiceType.HAIR_MAN_30){
                sql += "AND hair_cut_real_price>=3000 AND hair_cut_real_price<6000";
            }
            else if (serviceType == HairServiceType.HAIR_MAN_60){
                sql += "AND hair_cut_real_price>=6000 AND hair_cut_real_price<9000";
            }
            else if (serviceType == HairServiceType.HAIR_MAN_90){
                sql += "AND hair_cut_real_price>9000";
            }
        }



        sql += " AND status=?";
        paramList.add(BusinessStatusType.VERIFY_PASSED);

        if (listOrderType == FilterType.COMPLEX){
            sql += " ORDER BY order_count DESC";
        }
        else if (listOrderType == FilterType.FAVORABLE_COMMENT){
            sql += " ORDER BY favorable_comment_count DESC";
        }
        else if (listOrderType == FilterType.PRICE){
            sql += " ORDER BY hair_cut_real_price ASC";
        }

        sql += " limit ?,?";

        paramList.add(pageIndex * pageSize);
        paramList.add(pageSize);

        return selectList(null, sql, paramList);
    }

    @Override
    public Barber selectBarberById(long barberId) {
        return selectByPK(barberId, barberId);
    }

    @Override
    public void updateCollectCount(long barberId, boolean isCollected) {
        String sql;
        if (isCollected){
            sql = "UPDATE " + getTableName(barberId) + " SET collect_count = collect_count+1 WHERE id=?";
        }
        else {
            sql = "UPDATE " + getTableName(barberId) + " SET collect_count = collect_count-1 WHERE id=?";
        }
        execute(barberId, sql, barberId);
    }

    @Override
    public void updateComment(long barberId, int commentCount, int favorableCommentCount, int commentScore, int honor) {
        String sql = "UPDATE " + getTableName(barberId) + " SET comment_count=? , favorable_comment_count=? , comment_score=? , honor=? WHERE id=?";
        execute(barberId, sql, commentCount, favorableCommentCount, commentScore, honor, barberId);
    }

    @Override
    public List<Barber> searchBarberList(String searchText, int pageIndex, int pageSize) {
        if (StringUtil.isNullOrEmpty(searchText) || StringUtil.isNullOrEmpty(searchText.trim())){
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=? limit ?,?";
            return selectList(null, sql, BusinessStatusType.VERIFY_PASSED, pageIndex * pageSize, pageSize);
        }
        else {
            String sql = "SELECT * FROM " + getTableName(null) + " WHERE status=? AND nick_name like '%"+searchText.trim()+"%' limit ?,?";
            return selectList(null, sql, BusinessStatusType.VERIFY_PASSED, pageIndex * pageSize, pageSize);
        }
    }

    @Override
    public boolean isExistPhoneNumber(String phoneNumber) {
        String sql = "SELECT id FROM " + getTableName(null) + " WHERE phone_number=?";
        return isExist(null,sql, phoneNumber);
    }

    @Override
    public void updatePassword(String phoneNumber, String password, String passwordSalt) {
        String sql = "UPDATE " + getTableName(null) + " SET password=?, password_salt=? WHERE phone_number=?";
        execute(null, sql, password, passwordSalt, phoneNumber);
    }

    @Override
    public Barber selectBarberByPhone(String phoneNumber) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE phone_number=?";
        return selectOne(null,sql, phoneNumber);
    }

    @Override
    public void update(Barber barber) {
        updateByPK(null, barber);
    }

    @Override
    public List<Barber> selectShopBarberList(long shopId, int businessStatusType) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE shop_id=? AND status=?";
        return selectList(null, sql, shopId, businessStatusType);
    }

    @Override
    public void updateBarberStatus(long barberId, int businessStatusType) {
        String sql = "UPDATE " + getTableName(null) + " SET status=? WHERE id=?";
        execute(null, sql, businessStatusType, barberId);
    }

    @Override
    public void updateShopInfo(long shopId, String masterShopName, String slaveShopName, int cityId, int districtId, int businessCircleId, String address, BigDecimal longitude, BigDecimal latitude) {
        String sql = "UPDATE " + getTableName(null) + " SET master_shop_name=?, slave_shop_name=?, city_id=?,district_id=?,business_circle_id=?,address=?,longitude=?,latitude=? WHERE shop_id=?";
        execute(null, sql, masterShopName, slaveShopName, cityId, districtId, businessCircleId, address ,longitude, latitude, shopId);
    }
}
